package com.bdqn.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.pojo.SysRole;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysRoleService;
import com.bdqn.service.SysRoleServiceImpl;
import com.bdqn.service.SysUserService;
import com.bdqn.service.SysUserServiceImpl;
import com.bdqn.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/role")
public class SysRoleController extends HttpServlet {
    private SysRoleService service=new SysRoleServiceImpl();
    private SysUserService userService=new SysUserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码方式
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 控制参数
        String opr = request.getParameter("opr");
        switch (opr){
            case "list":
                list(request,response);
                break;
            case "del":
                delete(request,response);
                break;
            case "toadd":
                toadd(request,response);
                break;
            case "doadd":
                doadd(request,response);
                break;
            case "codeExist":
                codeExist(request,response);
                break;
            case "toUpdate":
                toUpdate(request,response);
                break;
            case "update":

                break;
        }
    }

    private void toUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id= Integer.parseInt(request.getParameter("id"));
        SysRole sysRole=service.getBySysRole(id);
        request.setAttribute("sysRole",sysRole);
        request.getRequestDispatcher("WEB-INF/jsp/sysRole/update.jsp").forward(request,response);
    }

    private void codeExist(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String code=request.getParameter("code");
        int exist=service.codeCount(code);
        Map<String,Integer>map=new HashMap<>();
        map.put("exist",exist);
        System.out.println(exist);
        String resultJson= JSON.toJSONString(map);
        response.getWriter().print(resultJson);
        response.getWriter().flush();
        response.getWriter().close();
    }

    private void doadd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SysRole sysRole=new SysRole();
        String code=request.getParameter("code");
        sysRole.setCode(code);
        String roleName=request.getParameter("roleName");
        sysRole.setRoleName(roleName);
        int row=service.add(sysRole);
        if (row>0){
            response.getWriter().print("<script>alert('新增成功');window.location='role?opr=list';</script>");
        }
        response.getWriter().flush();
        response.getWriter().close();
    }

    private void toadd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/jsp/sysRole/add.jsp").forward(request,response);
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id= Integer.parseInt(request.getParameter("id"));
        String delResult="notexist";
        if (userService.getRoleId(id)>0){
            delResult="false";
        }else {
            if (service.deleteRole(id)>0){
                delResult="true";
            }else {
                delResult="notexist";
            }
        }
        Map<String,String> map=new HashMap<>();
        map.put("delResult",delResult);
        String Json= JSON.toJSONString(map);
        response.getWriter().print(Json);
        response.getWriter().flush();
        response.getWriter().close();

    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取页面查询参数
        String pageIndex = request.getParameter("pageIndex");

        // 分页参数（默认查询第1页）
        int pageNo = (pageIndex == null || "".equals(pageIndex)) ? 1 : Integer.parseInt(pageIndex);

        // 构建分页对象
        Page<SysRole> page = new Page<>();
        // 查询总数据量
        int count = service.count();
        page.setTotalCount(count); // 总数据量
        // 先设置总数据量再设置页码
        page.setCurrPageNo(pageNo);
        // 查询分页数据
        List<SysRole> pageList = service.queryPageByRealNameAndRoleId( page.getCurrPageNo()-1,page.getPageSize());
        // 分页数据
        request.setAttribute("roleList", pageList);
        request.setAttribute("param",page);
        request.getRequestDispatcher("WEB-INF/jsp/sysRole/list.jsp").forward(request,response);
    }
}
